package com.validate.fluentvalidator.reflection;

import com.validate.advocatevalidator.GuideInitialize;
import com.validate.advocatevalidator.ValidatorsList;
import com.validate.advocatevalidator.ValidatorsResult;
import com.validate.annotation.Valid;
import com.validate.annotation.ValidatorsHandler;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


public class FieldReflection<T> extends GuideInitialize {


	/**校验总列表，存储单个字段需要被多少校验器校验**/
	private List<ValidatorsList> listvcm = new ArrayList<ValidatorsList>();

	/**
	 * 校验
	 *
	 * @param WXO 对象
	 * @return
	 */
	public T reflection(Object WXO) {
		return reflection(WXO, new String[]{},false);
	}

	/**
	 * 校验
	 *
	 * @param WXO   对象
	 * @param group 分组
	 * @return
	 */
	public T reflection(Object WXO, String[] group,boolean allnext) {
		try {
			nextValid(WXO,"");
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return (T)validator(group,false);
	}


	/**
	 * 循环获取到Valid注解以及普通字段
	 * @param WXO 对象
	 * @param paterfield 父对象
	 * @throws IllegalAccessException
	 */
	private void nextValid(Object WXO,String paterfield) throws IllegalAccessException {
		//对象不能为NULL
		if(WXO == null){
			throw new IllegalAccessException("Object Is Null");
		}
		Class cls = WXO.getClass();
		//获取所有字段
		Field[] fields = cls.getDeclaredFields();
		for (Field f : fields) {
			f.setAccessible(true);
			//过滤非字段数据
			if (f.getName().indexOf("$") == -1) {
				//判断是否要进行子循环
				Valid v = f.getAnnotation(Valid.class);
				if (v != null) {
					nextValid(f.get(WXO),f.getName());
				} else {
					//获取字段的所有注解
					Annotation[] annotations = f.getAnnotations();
					//单字段校验列表
					ValidatorsList vcm = new ValidatorsList();
					//校验器列表
					List<ValidatorsHandler> listvcmh = new ArrayList<ValidatorsHandler>();
					//获取到反射的字段的名称和值
					vcm.setField(f.getName()).setValue(f.get(WXO)).setPaterfield(paterfield);
					//循环注解，获取注解的校验器
					for (Annotation annotation : annotations) {
						ValidatorsHandler[] validatorsHandler = validatorsHandlers(annotation.annotationType().getName(), f);
						if (validatorsHandler != null && validatorsHandler.length > 0) {
							for (ValidatorsHandler validatorsHandler1 : validatorsHandler) {
								validatorsHandler1.field(f);
								listvcmh.add(validatorsHandler1);
							}
						}
					}
					vcm.setListValidatorsHandler(listvcmh);
					listvcm.add(vcm);
				}
			}
		}
	}


	/**
	 * 分组校验，校验到一个错误立即返回
	 *
	 * @param groups
	 * @param allnext 默认false出现错误理解返回 true校验完成后判断错误
	 * @return
	 */
	private ValidatorsResult validator(String[] groups,boolean allnext) {
		ValidatorsResult valRest = new ValidatorsResult();
		//校验结果
		List<ValidatorsResult> allnetValidatorsResult = new ArrayList<ValidatorsResult>();
		for (ValidatorsList valcmodel : listvcm) {
			List<ValidatorsHandler> listvhs = valcmodel.getListValidatorsHandler();
			for (ValidatorsHandler listvh : listvhs) {
				//分组校验
				if (groups.length > 0) {
					for (String group : groups) {
						if (!group.equals(listvh.group())) {
							break;
						}
					}
				}
				//校验
				if (listvh.validate(valcmodel.getValue())) {
					//是否继续下一轮校验
					if (!listvh.next()) {
						break;
					}
				} else {
					if(allnext){
						allnetValidatorsResult.add(listvh.Result());
					}else {
						return listvh.Result();
					}
				}
			}
		}
		//校验列表内有队列返回true，没有就返回false
		if (listvcm.size() > 0) {
			valRest.setSuccess(true);
		} else {
			valRest.setSuccess(false);
		}
		return valRest;
	}

}
